using System;
using System.Collections.Generic;
using Microsoft.Practices.Repository;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using DataAccess.BusinessEntities;
using DataAccess.Interfaces;

namespace RepositoryFactoryTestApp.Tests
{
    [TestFixture]
    public class RepositoryTests
    {
        private string _connection = "MyConnection";

        [Test]
        public void Customer_CRUD()
        {
            ICustomerRepository repository = RepositoryFactory.Create<ICustomerRepository>(_connection);
            List<Customer> customers = repository.GetAllFromCustomer();

            // Create
            Customer customer = new Customer();
            customer.FirstName = "First1";
            customer.LastName = "Last1";
            customer.Company = "Company1";
            customer.Email = "user1@domain.com";
            repository.Add(customer);

            // Read
            List<Customer> customers2 = repository.GetAllFromCustomer();
            Assert.That(customers2.Count, Is.EqualTo(customers.Count + 1), "Total number of records mismatch.");

            // Update
            customer = customers2[customers2.Count - 1];
            customer.FirstName = "First2";
            customer.LastName = "Last2";
            customer.Company = "Company2";
            customer.Email = "user2@domain.com";
            repository.Save(customer);
            customers2 = repository.GetAllFromCustomer();
            Assert.That(customers2[customers2.Count - 1].Company, Is.EqualTo("Company2"));

            // Delete
            repository.Remove(customer.CustomerId);
            customers2 = repository.GetAllFromCustomer();
            Assert.That(customers.Count, Is.EqualTo(customers2.Count), "Total number of records mismatch.");
        }

        [Test]
        public void Album_CRUD()
        {
            IAlbumRepository repository = RepositoryFactory.Create<IAlbumRepository>(_connection);
            List<Album> albums = repository.GetAllFromAlbum();

            // Create
            Album album = new Album();
            album.Title = "Name1";
            album.ArtistId = 1;
            repository.Add(album);

            // Read All
            List<Album> albums2 = repository.GetAllFromAlbum();
            Assert.That(albums2.Count, Is.EqualTo(albums.Count + 1), "Total number of records mismatch.");

            // Read One
            int albumId = albums2[albums2.Count - 1].AlbumId;
            album = repository.GetAlbumByAlbumId(albumId);
            Assert.That(album.Title, Is.EqualTo("Name1"));

            // Update
            album = albums2[albums2.Count - 1];
            album.Title = "Name2";
            repository.Save(album);
            albums2 = repository.GetAllFromAlbum();
            Assert.That(albums2[albums2.Count - 1].Title, Is.EqualTo("Name2"));

            // Delete
            repository.Remove(album.AlbumId);
            albums2 = repository.GetAllFromAlbum();
            Assert.That(albums2.Count, Is.EqualTo(albums.Count), "Total number of records mismatch.");
        }

        [Test]
        public void Invoice_CRUD()
        {
            IInvoiceRepository repository = RepositoryFactory.Create<IInvoiceRepository>(_connection);
            List<Invoice> invoices = repository.GetAllFromInvoice();

            // Create
            Invoice invoice = new Invoice();
            invoice.CustomerId = 1;
            invoice.EmployeeId = 1;
            invoice.InvoiceDate = DateTime.Today;
            repository.Add(invoice);

            // Read
            List<Invoice> invoices2 = repository.GetAllFromInvoice();
            Assert.That(invoices2.Count, Is.EqualTo(invoices.Count + 1), "Total number of records mismatch.");

            // Update
            invoice = invoices2[invoices2.Count - 1];
            invoice.CustomerId = 2;
            repository.Save(invoice);
            invoices2 = repository.GetAllFromInvoice();
            Assert.That(invoices2[invoices2.Count - 1].CustomerId, Is.EqualTo(2));

            // Delete
            repository.Remove(invoice.InvoiceId);
            invoices2 = repository.GetAllFromInvoice();
            Assert.That(invoices2.Count, Is.EqualTo(invoices.Count), "Total number of records mismatch.");
        }

        [Test]
        public void InvoiceLine_CRUD()
        {
            IInvoiceLineRepository repository = RepositoryFactory.Create<IInvoiceLineRepository>(_connection);
            List<InvoiceLine> invoiceLines = repository.GetAllFromInvoiceLine();

            // Create
            InvoiceLine invoiceLine = new InvoiceLine();
            invoiceLine.InvoiceId = 1;
            invoiceLine.TrackId = 1;
            repository.Add(invoiceLine);

            // Read
            List<InvoiceLine> invoiceLines2 = repository.GetAllFromInvoiceLine();
            Assert.That(invoiceLines2.Count, Is.EqualTo(invoiceLines.Count + 1), "Total number of records mismatch.");

            // Update
            invoiceLine = invoiceLines2[invoiceLines2.Count - 1];
            invoiceLine.TrackId = 2;
            repository.Save(invoiceLine);
            invoiceLines2 = repository.GetAllFromInvoiceLine();
            Assert.That(invoiceLines2[invoiceLines2.Count - 1].TrackId, Is.EqualTo(2));

            // Delete
            repository.Remove(invoiceLine.InvoiceLineId);
            invoiceLines2 = repository.GetAllFromInvoiceLine();
            Assert.That(invoiceLines2.Count, Is.EqualTo(invoiceLines.Count), "Total number of records mismatch.");
        }

    }
}
